package com.wtone.platform.gat.images.gat.message;

import com.wtone.platform.gat.images.gat.model.SubImageInfo;
import com.wtone.platform.gat.images.gat.utils.BaseImg;
import com.wtone.platform.gat.images.gat.utils.DateUtils;
import com.wtone.platform.gat.images.gat.utils.ImageWatermarkUtil;
import com.wtone.platform.gat.images.gat.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: aiLin
 * @Date: 2020/9/26 17:18
 * @desc: 图片信息 处理下载
 *
 * "ImageID": "31000051511191251218992018102510500055558",
 * "EventSort": 1,
 * "DeviceID": "31000051511191251218",
 * "StoragePath": "https://10.129.35.207:8090/VIID/SubImages/L21udC96amgvZGF0YS9WSUlEL2ltYWdlL25vbm1vdG9ydmVoaWNsZS8zMTAwMDA1MTUxMTE5MTI1MTIxOC8yMDE4MTIyMi8zMTAwMDA1MTUxMTE5MTI1MTIxODk5MjAxODEwMjUxMDUwMDA1NTU1NS5KcGVn",
 * "Type": "11",
 * "FileFormat": "Jpeg",
 * "ShotTime": "20181222100900"
 */
@Slf4j
@Component
public class SubImageInfoCore {

    private static String IMGPATH;
    @Value("${img.imgPath}")
    public void setPROFILES(String profile) {
        this.IMGPATH = profile;
    }

    public static void getSubImageInfo(List<SubImageInfo> subImageInfo){
        if(subImageInfo==null || subImageInfo.size()<=0){
            return;
        }
        for(int k=0;k<subImageInfo.size();k++){
            SubImageInfo subImg = subImageInfo.get(k);
            if(subImg==null){
                continue;
            }
            String storagePath = subImg.getStoragePath();
            String data =subImg.getData();
            if(StringUtils.isNotEmpty(storagePath)){
                //log.warn("网络下载图片下发：{}",filePath);
                String fileName = DateUtils.dateTimeNows()+".jpg";
                String filePath = ImageWatermarkUtil.downloadPicture(IMGPATH,"HTTP-"+fileName,storagePath);
                log.info("filePath:"+filePath);
                continue;
            }else if(StringUtils.isNotEmpty(data)){
                //log.warn("网络下载图片下发：{}",filePath);
                String fileName = DateUtils.dateTimeNows()+".jpg";
                try {
                    Thread.sleep(1000);
                    BaseImg.toImg(data,IMGPATH+"BASE64-"+fileName);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //log.info("data.length():"+data.length());
                continue;
            }else {
                log.info("图片地址为空，略过下载");
            }
        }
    }
}
